package com.hyk.commonLib.common.utils.dataBinding.bindingAdapter;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.hyk.commonLib.LibInitializer;
import com.hyk.commonLib.common.entity.ImageSource;
import com.hyk.commonLib.common.utils.ImageLoader;
import com.hyk.commonLib.common.utils.ResUtils;

/**
 * Created by 贺玉琨 on 2020/7/15.
 */
public class OnLoadImageAdapter {
    @BindingAdapter(value = {"loadImg", "placeDrawable", "showPlaceholder"}, requireAll = false)
    public static void loadImg(ImageView imageView, String imageUrl, Drawable placeDrawable, boolean showPlaceholder) {
        if (TextUtils.isEmpty(imageUrl)) {
            imageView.setImageDrawable(null);
            return;
        }
        if (showPlaceholder) {
            if (placeDrawable == null) {
                placeDrawable = LibInitializer.getLoadImgPlaceholder();
            }
            ImageLoader.load(imageUrl, imageView, ImageLoader.Option.get().setPlaceholder(ImageSource.fromDrawable(placeDrawable)));
        } else {
            ImageLoader.load(imageUrl, imageView);
        }
    }

    @BindingAdapter(value = {"loadImg", "placeDrawable", "showPlaceholder"}, requireAll = false)
    public static void loadImg(ImageView imageView, ImageSource imageSource, Drawable placeDrawable, boolean showPlaceholder) {
        if (imageSource == null) {
            imageView.setImageDrawable(null);
            return;
        }
        if (showPlaceholder) {
            if (placeDrawable == null) {
                placeDrawable = LibInitializer.getLoadImgPlaceholder();
            }
            ImageLoader.load(imageSource, imageView, ImageLoader.Option.get().setPlaceholder(ImageSource.fromDrawable(placeDrawable)));
        } else {
            ImageLoader.load(imageSource, imageView);
        }
    }

    @BindingAdapter(value = {"loadImg", "placeDrawable", "showPlaceholder"}, requireAll = false)
    public static void loadImg(ImageView imageView, Drawable imgDrawable, Drawable placeDrawable, boolean showPlaceholder) {
        imageView.setImageDrawable(imgDrawable);
    }

    @BindingAdapter(value = {"loadImg", "placeDrawable", "showPlaceholder"}, requireAll = false)
    public static void loadImg(ImageView imageView, int imageRes, Drawable placeDrawable, boolean showPlaceholder) {
        imageView.setImageDrawable(ResUtils.getDrawable(imageRes));
    }
}
